package com.jelly.web.modules.system.role.controller;

import com.jelly.common.constant.GlobalConstant;
import com.jelly.common.note.OperationLog;
import com.jelly.common.util.ListUtil;
import com.jelly.common.util.StringUtils;
import com.jelly.common.util.web.AjaxPostResponse;
import com.jelly.common.web.page.LayuiTableInfo;
import com.jelly.common.web.tree.Tree;
import com.jelly.web.share.base.controller.BaseController;
import com.jelly.web.share.base.domain.Base;
import com.jelly.web.modules.system.permission.domain.Permission;
import com.jelly.web.modules.system.permission.service.IPermissionService;
import com.jelly.web.modules.system.role.domain.Role;
import com.jelly.web.modules.system.role.service.IRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @创建人 ql
 * @创建时间 2018-09-14 11:28
 * @项目名称 jelly
 * @功能描述: 角色管理控制器
 **/
@Controller
@RequestMapping("/admin/system/role")
public class RoleController extends BaseController {

    @Autowired
    private IRoleService roleService;

    @Autowired
    private IPermissionService permissionService;

    /**
     * 角色列表跳转
     * @return
     */
    @GetMapping
    public String getRoleList(){

        //获取字典详情
        getDictionary();

        //获取该用户可访问的按钮详情
        getButton();

        return adminSystemRole("/list");

    }

    /**
     * 角色列表详情
     * @param role
     * @return
     */
    @PostMapping
    @ResponseBody
    public LayuiTableInfo getRoleList(Role role){

        getPageHelper();

        return getPageHelper(roleService.getRole(role));

    }

    /**
     * 新增角色跳转
     * @return
     */
    @GetMapping("/add")
    public String addRole(Model model){
        //获取字典详情
        getDictionary();
        //获取所有权限
        Permission permission = new Permission();
        Tree<Permission> tree = permissionService.getTreePermission(permission);
        if(null != tree && ListUtil.isNotList(tree.getChildren())){
            model.addAttribute("tree",tree.getChildren());
        }
        return adminSystemRole("/add");

    }

    /**
     * 新增角色
     * @param role
     * @return
     */
    @PostMapping("/add")
    @OperationLog(name = "角色模块",value = GlobalConstant.ADDED)
    @ResponseBody
    public ResponseEntity<AjaxPostResponse> addUser(Role role){

        if(roleService.addRole(role)){

            return okResponse("新增成功");

        }

        return errorResponse("新增失败");

    }

    /**
     * 修改角色跳转
     * @param model
     * @param base
     * @return
     */
    @GetMapping("/update")
    public String updateRole(Model model, Base base){
        //获取字典详情
        getDictionary();

        if(StringUtils.isNotBlank(base.getBaseId())){
            model.addAttribute("role",roleService.getRoleId(base.getBaseId()));
            model.addAttribute("tree",roleService.getTreeRolePermission(base.getBaseId()));
        }

        return adminSystemRole("/update");

    }

    /**
     * 修改角色
     * @param role
     * @return
     */
    @PostMapping("/update")
    @OperationLog(name = "角色模块",value = GlobalConstant.UPDATE)
    @ResponseBody
    public ResponseEntity<AjaxPostResponse> updateRole(Role role){

        if(roleService.updateRole(role)){

            return okResponse("修改成功");

        }

        return errorResponse("修改失败");

    }


    /**
     * 删除角色
     * @param base
     * @return
     */
    @PostMapping("/delect")
    @OperationLog(name = "角色模块",value = GlobalConstant.DELECT)
    @ResponseBody
    public ResponseEntity<AjaxPostResponse> delectRole(Base base){

        if(StringUtils.isNotBlank(base.getBaseId())){

            if(roleService.delectRole(base.getBaseId())){

                return okResponse("删除成功");

            }

        }

        return errorResponse("删除失败");

    }

    /**
     * 角色信息验证
     * @param role
     * @return
     */
    @PostMapping("/verify")
    @ResponseBody
    public ResponseEntity<AjaxPostResponse> userVerify(Role role){

        if(StringUtils.isNotBlank(role.getRoleRoute()) || StringUtils.isNotBlank(role.getRoleName())){
            List<Role> list = roleService.getRole(role);
            if(ListUtil.isNotList(list)){
                if(StringUtils.isNotBlank(role.getRoleRoute())){
                    return errorResponse("角色名称重复");
                }else{
                    return errorResponse("角色标识重复");
                }
            }
        }
        return okResponse("");

    }

}
